<?php

namespace App\Store\Models\Financial\Statements;

use App\Common\Services\Common;
use App\Store\Models\Account\SiteStore;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;

class Voucher extends Model
{
    protected $table = 'site_financial_statements_voucher';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     *获取列表
     */
    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::query()
            ->with(['storeinfo','subject'=>function ($query)use($cp_id) {
                $query->where(function ($query) use ($cp_id) {
                    $query->where('cp_id', $cp_id)
                        ->orWhere('cp_id', 0);
                });
            }])
            ->where([['is_del', 1], ['cp_id', $cp_id]]);

        $serial_number = $request['serial_number'] ?? '';
        if ($serial_number != '') {
            $query->where('serial_number', '=', $serial_number);
        }

        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);

        if ($start_time>0&& $end_time>0) {
            $query->whereBetween('voucher_date', [$start_time, $end_time]);
        }

        $store_id =$request->store_id??0;
        if ($store_id>0) {
            $query->where('store_id', $store_id);
        }

        $query->orderBy('create_at', 'desc');
        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    public function getvoucherNumber($year, $month)
    {
        $info = Voucher::query()
            ->where('year', $year)
            ->where('month', $month)
            ->where('is_del', 1)
            ->orderBy('id', 'desc')
            ->first();

        $info = empty($info) ? [] : $info->toArray();
        $month =strval((int)$month);
        if (empty($info)) {
            $voucher_number = "$month-1";
        } else {
            $arr = explode('-', $info['voucher_number']);
            $n =strval((int)($arr[1] ?? 0) + 1);
            $voucher_number ="$month-$n";
        }

        return $voucher_number;
    }



    public function storeinfo()
    {

        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id', 'store_name');
    }

    public function subject()
    {
        return $this->hasOne(Subject::class, 'serial_number', 'serial_number')->where('serial_number','<>','')->select('id', 'serial_number','subject_name');
    }


    public function getVoucherList($param)
    {
      return Voucher::query()
          ->where('is_del', 1)
          ->where($param)
          ->get()
          ->toArray();
    }

}

